package com.cskaoyan.controller.systemcontroller;

import com.cskaoyan.aspect.LogA;
import com.cskaoyan.exceptionJY.NameRepeatException;
import com.cskaoyan.exceptionJY.RoleExistException;
import com.cskaoyan.model.bean.Admin;
import com.cskaoyan.model.bean.Role;
import com.cskaoyan.model.bean.Storage;
import com.cskaoyan.model.bo.systembo.*;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.systemvo.*;
import com.cskaoyan.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class SystemController {

    @Autowired
    SystemService systemService;

    //管理员列表
    @RequestMapping("/admin/admin/list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String username){
        AdminPageVo vo = systemService.queryPage(page, limit, sort, order, username);
        return BaseRespVo.ok(vo);
    }

    //创建管理员
    @RequestMapping("/admin/admin/create")
    @LogA
    public BaseRespVo create(@RequestBody @Valid CreateAdminBo createAdminBo, BindingResult bindingResult) throws NameRepeatException {
        if (bindingResult.hasFieldErrors()){
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return BaseRespVo.error(601,defaultMessage);
        }
        Admin admin = systemService.createAdmin(createAdminBo);
        return BaseRespVo.ok(admin);
    }

    //删除管理员
    @RequestMapping("/admin/admin/delete")

    @LogA
    public BaseRespVo delete(@RequestBody DeleteAdminBo deleteAdminBo){
        systemService.deleteAdmin(deleteAdminBo);
        return BaseRespVo.ok();
    }

    //编辑管理员啊
    @RequestMapping("/admin/admin/update")
    @LogA
    public BaseRespVo update(@RequestBody @Valid UpdateAdminBo updateAdminBo,BindingResult bindingResult) throws NameRepeatException {
        if (bindingResult.hasFieldErrors()){
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return BaseRespVo.error(601,defaultMessage);
        }
        Admin admin = systemService.updateAdmin(updateAdminBo);
        return BaseRespVo.ok(admin);
    }


    //操作日志的显示
    @RequestMapping("/admin/log/list")

    public BaseRespVo logList(Integer page, Integer limit, String sort, String order, String name){
        LogPageVo logPageVo = systemService.logList(page,limit,sort,order,name);
        return BaseRespVo.ok(logPageVo);
    }

    //角色列表
    @RequestMapping("/admin/role/list")

    public BaseRespVo roleList(Integer page, Integer limit, String sort, String order, String name){
        RolePageVo rolePageVo = systemService.roleList(page, limit, sort, order, name);
        return BaseRespVo.ok(rolePageVo);
    }

    //角色类型显示
    @RequestMapping("/admin/role/options")
    public BaseRespVo options(){
        ArrayList<OptionVo> optionVos = systemService.roleOption();
        return BaseRespVo.ok(optionVos);
    }

    //创建角色
    @RequestMapping("/admin/role/create")

    public BaseRespVo roleCreate(@RequestBody CreateRoleBo createRoleBo) throws NameRepeatException {
        Role role = systemService.roleCreate(createRoleBo);
        return BaseRespVo.ok(role);
    }

    //删除角色
    @RequestMapping("/admin/role/delete")

    public BaseRespVo roleDelete(@RequestBody DeleteRoleBo deleteRoleBo) throws RoleExistException {
        systemService.roleDelete(deleteRoleBo);
        return BaseRespVo.ok();
    }

    //编辑角色
    @RequestMapping("/admin/role/update")

    public BaseRespVo roleUpdate(@RequestBody UpdateRoleBo updateRoleBo){
        systemService.updateRole(updateRoleBo);
        return BaseRespVo.ok();
    }

    //角色授权get
    @GetMapping("/admin/role/permissions")
    public BaseRespVo rolePerGet(Integer roleId){
        PermissionPageVo permissionPageVo = systemService.rolePerGet(roleId);
        return BaseRespVo.ok(permissionPageVo);
    }

    //角色授权post
    @PostMapping("/admin/role/permissions")
    public BaseRespVo rolePerPost(@RequestBody PermissionBo permissionBo){
        systemService.rolePerPost(permissionBo);
        return BaseRespVo.ok();
    }

    //图片上传
    @RequestMapping({"/admin/storage/create", "/wx/storage/upload"})
    public BaseRespVo storageCreate(MultipartFile file){
        Storage storage = systemService.storageCreate(file);
        return BaseRespVo.ok(storage);
    }

    //对象列表
    @RequestMapping("/admin/storage/list")
    public BaseRespVo storageList(Integer page, Integer limit, String sort, String order, String key, String name){
        StoragePageVo storagePageVo = systemService.storageList(page,limit,sort,order,key,name);
        return BaseRespVo.ok(storagePageVo);
    }

    //对象删除
    @RequestMapping("/admin/storage/delete")
    public BaseRespVo storageDelete(@RequestBody DeleteStorageBo deleteStorageBo){
        systemService.storageDelete(deleteStorageBo);
        return BaseRespVo.ok();
    }

    //对象编辑
    @RequestMapping("/admin/storage/update")
    public BaseRespVo storageUpdate(@RequestBody Storage storage){
        Storage storage1 = systemService.storageUpdate(storage);
        return BaseRespVo.ok(storage1);
    }


}
